<template>
	<view class="content" :style="'height:'+height">
		<view class="top-bar">
			<view class="top-bar-left" @tap="backOne">
				<image src="../../static/img/group/back-white.png"></image>
			</view>
			<view class="top-bar-right">
				<image src="../../static/img/group/more-white.png"></image>
			</view>
		</view>
		<view class="top-bar blackbar" :animation="animationData1">
			<view class="top-bar-left" @tap="backOne">
				<image src="../../static/img/group/back.png"></image>
			</view>
			<view class="top-bar-right">
				<image src="../../static/img/group/more.png"></image>
			</view>
		</view>
		<view class="bg">
			<image :src="group.cover" mode="aspectFill"></image>
		</view>
		<view class="main">
			<view class="group-info">
				<view class="info">
					<view class="name">
						{{group.name}}
					</view>
					<view class="time">
						{{group.time}}
					</view>
					<view class="notice">
						{{group.notice}}
					</view>
				</view>
				<view class="member">
					<view class="title">
						<view class="title-left">
							群成员
						</view>
						<view class="title-right">
							成员管理 <i class="iconfont icon-arrow-right"></i>
						</view>
					</view>
					<view class="member-list">
						<view class="member-item" v-for="(item,i) in user" :key="i">
							<view class="avatar">
								<image :src="item.avatar"></image>
								<image src="../../static/img/group/X.png" @tap="deluser(item.id)"
									v-if="type&&group.userId!=item.id"></image>
							</view>
							<view class="name">
								{{item.name}}
							</view>
						</view>
						<view class="member-item" @tap="invite">
							<view class="add">
								<image src="../../static/img/group/add.png"></image>
							</view>
							<view class="name" >
								邀请
							</view>
						</view>
					</view>
					<view class="group-list">
						<view class="row" v-if="type">
							<view class="shuxing">
								群名称
							</view>
							<view class="con">
								<Tinput :text.sync="group.name" @change="change(1)"></Tinput>
							</view>
							<view class="more">
								<image src="../../static/img/common/right.png" mode="aspectFill"></image>
							</view>
						</view>
						<view class="row" v-if="type">
							<view class="shuxing">
								群头像
							</view>
							<view class="con">
								<ImageCropper :src="tempFilePath" @confirm="confirm" @cancel="cancel"></ImageCropper>
								<image :src="group.cover" @tap="upload" class="img"></image>
							</view>
							<view class="more">
								<image src="../../static/img/common/right.png" mode="aspectFill"></image>
							</view>
						</view>
						<view class="row" v-if="type">
							<view class="shuxing">
								群公告
							</view>
							<view class="con" @tap="changenotice">
								<text>{{group.notice}}</text>
							</view>
							<view class="more">
								<image src="../../static/img/common/right.png" mode="aspectFill"></image>
							</view>
						</view>
						<view class="row">
							<view class="shuxing">
								群昵称
							</view>
							<view class="con">
								<Tinput :text.sync="me.name" @change="changenick"></Tinput>
							</view>
							<view class="more">
								<image src="../../static/img/common/right.png" mode="aspectFill"></image>
							</view>
						</view>
						<view class="row xiaoxi">
							<view class="right">
								消息免打扰
							</view>
							<view class="left">
								<switch :checked="me.shield" color="#FFE431" @change="checkchange" />
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 修改框 -->
		<view class="midify" :animation="animationData">
			<view v-if="isedit">
				<view class="midify-header">
					<view class="close" @tap="close">
						取消
					</view>
					<view class="title">
						修改
					</view>
					<view class="define" @tap="midify">
						确定
					</view>
				</view>
				<view class="midify-main">
					<textarea class="midify-content" v-model="announcement" :placeholder="'请输入新的公告'"></textarea>
				</view>
			</view>
			<view v-else>
				<view class="midify-header">
					<view class="close" @tap="close">
						取消
					</view>
					<view class="title">
						邀请新成员
					</view>
					<view class="define" @tap="add">
						确定
					</view>
				</view>
				<view class="group-list">
					<view class="member" v-for="(item,i) in friend" :key="i">
						<view class="choose" :style="{background:item.choose?'#FFE431':'rgba(255,228,49,0.50)'}"
							@tap="choose(i)">
							<i class="iconfont icon-duihao dui" v-if="item.choose"></i>
						</view>
						<view class="avatar">
							<image :src="item.avatar"></image>
						</view>
						<view class="username">
							{{item.name}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="group-bottom">
			<view class="group-btn" @tap="back">
				{{type?'解散群聊':'退出群聊'}}
			</view>
		</view>
	</view>
</template>

<script>
	import Tinput from '@/components/Tinput/Tinput.vue'
	import ImageCropper from '@/components/ling-imgcropper/ling-imgcropper.vue'
	import animation from '@/common/js/animation.js'
	import {
		ymd
	} from '../../common/js/DateFormate.js'
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				announcement: '',
				tempFilePath: "",
				animationData: '',
				animationData1: '',
				height: '',
				user: [],
				istop: false,
				groupid: '',
				userid: '',
				group: {},
				check: true,
				me: {},
				isedit:false,
				friend:[]
			};
		},
		components: {
			Tinput,
			ImageCropper
		},
		methods: {
			//获取群成员信息
			async getuser() {
				var {
					data
				} = await this.request({
					url: '/group/user',
					data: {
						id: this.groupid
					}
				})
				if (data.status == 200) {
					this.user = data.data
				}
				this.user.forEach(item => {
					if (item.id == this.id) {
						this.me = item
					}
				})
			},
			async getgroup() {
				var {
					data
				} = await this.request({
					url: '/group',
					data: {
						id: this.groupid
					}
				})
				if (data.status == 200) {
					this.group = data.data
				}
				this.group.time = ymd(this.group.time)
			},
			backOne() {
				uni.navigateBack()
			},
			upload() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ["album", "camera"], //从相册选择
					success: (res) => {
						this.tempFilePath = res.tempFilePaths.shift();
					},
				});
			},
			confirm(e) {
				this.tempFilePath = "";
				this.cropFilePath = e.detail.tempFilePath;

				uni.uploadFile({
					url: "http://localhost:3000/files/upload",
					filePath: this.cropFilePath,
					name: "file",
					fileType: "image",
					formData: {
						id: this.id
					},
					success: (uploadFileRes) => {
						this.cropFilePath = uploadFileRes.data
						this.edit({
							cover: this.cropFilePath
						})
						//自定义操作
					},
					fail(e) {
						console.log("this is errormes " + e.message);
					},
				});

			},
			cancel() {
				console.log("canceled");
				this.tempFilePath = "";
			},
			async edit(obj) {
				await this.request({
					url: '/group',
					method: "PUT",
					data: {
						id: this.groupid,
						obj: obj
					}
				})
				this.getgroup()
			},
			change(e) {
				if (e == 1) {
					this.edit({
						name: this.group.name
					})
				} else {
					this.edit({
						notice: this.announcement
					})
				}
			},
			async editme(obj) {
				await this.request({
					url: '/group/user',
					method: "PUT",
					data: {
						uid: this.id,
						gid: this.groupid,
						obj: obj
					}
				})
				this.getuser()
			},
			checkchange(e) {
				var shield = e.detail.value
				this.editme({
					shield: shield
				})
			},
			changenick() {
				this.editme({
					name: this.me.name
				})
			},
			changenotice(){
				this.isedit = true
				this.announcement = this.group.notice
				this.open()
			},
			invite(){
				this.friend = this.FriendList
				this.user.forEach(item=>{
					for(let i in this.friend){
						this.friend[i].choose = false
						if(item.id == this.friend[i]._id){
							this.friend.splice(i,1)
						}
					}
				})
				this.isedit = false
				this.open()
			},
			choose(i){
				var data = this.friend[i]
				data.choose = !data.choose
				this.friend.splice(i,1,data)
			},
			add(){
				this.close()
				this.addmember()
			},
			async addmember(){
				var userlist = []
				this.friend.forEach(item=>{
					if(item.choose == true){
						userlist.push({id:item._id})
					}
				})
				if(userlist.length == 0) return
				var {data} = await this.request({
					url:'/group/add',
					method:'POST',
					data:{
						userlist:userlist,
						gid:this.groupid
					}
				})
				this.getuser()
			},
			// 弹出动画
			open() {
				this.height = '100vh'
				animation(a => {
					a.top(0).step()
					this.animationData = a.export()
				})
			},
			//关闭动画
			close() {
				this.height = '100%'
				animation(a => {
					a.top('100%').step()
					this.animationData = a.export()
				})
			},
			//确定修改
			midify() {
				this.change(2)
				this.close()
			},
			getrpx() {
				var obj = uni.getSystemInfoSync()
				var width = obj.windowWidth
				return width / 750
			},
			async back(){
				if(this.type){
					this.dissolution()
				}else{
					var {data} = await this.request({
						url:'/group/user',
						method:'DELETE',
						data:{
							uid:this.id,
							gid:this.groupid
						}
					})
					if(data.status ==200){
						uni.reLaunch({
							url:'/pages/index/index'
						})
					}else{
						uni.showToast({
							title: '退出失败，请重试',
							icon:error
						});
					}
				}
			},
			async deluser(id){
				var {data} = await this.request({
					url:'/group/user',
					method:'DELETE',
					data:{
						uid:id,
						gid:this.groupid
					}
				})
				this.getuser()
			},
			async dissolution(){
				var {data} = await this.request({
					url:'/group/all',
					method:'DELETE',
					data:{
						gid:this.groupid
					}
				})
				if(data.status ==200){
					uni.reLaunch({
						url:'/pages/index/index'
					})
				}else{
					uni.showToast({
						title: '退出失败，请重试',
						icon:error
					});
				}
			}
		},
		onLoad(e) {
			this.groupid = e.id
			this.getuser()
			this.getgroup()
		},
		onPageScroll(obj) {
			var rpx = this.getrpx()
			if (obj.scrollTop > (306 - 10) * rpx) {
				this.istop = true
			} else {
				this.istop = false
			}
		},
		watch: {
			istop(n) {
				var animation = uni.createAnimation({
					duration: 100,
					timingFunction: "ease",
					delay: 0
				})
				if (n) {
					animation.opacity(1).step()
					this.animationData1 = animation.export()
				} else {

					animation.opacity(0).step()
					this.animationData1 = animation.export()
				}
			}
		},
		computed: {
			...mapState([
				'id','FriendList'
			]),
			type() {
				if (this.group.userId == this.id) {
					return true
				} else {
					return false
				}
			}
		}
	}
</script>

<style lang="scss">
	@import "../../common/css/base.scss";

	.content {
		position: relative;
		box-sizing: border-box;
		overflow: hidden;
	}

	.top-bar {
		@include top-bar($bc:transparent, $bs:none);

		.top-bar-left,
		.top-bar-right {
			display: flex;
			justify-content: center;
			align-items: center;
			position: relative;
			padding: 0;
		}

		.img-black {
			position: absolute;
			top: 0;
			left: 0;
		}

		image {
			width: 68rpx;
			height: 68rpx;
		}
	}

	.blackbar {
		width: 750rpx;
		box-sizing: border-box;
		background-color: #FFF;
		opacity: 0;
	}

	.bg {
		position: fixed;
		width: 100%;
		height: 100%;
		background-color: #fff;
		z-index: -1;

		image {
			width: 100vw;
			height: 100vw;
		}
	}

	.main {
		padding: 360rpx 0 110rpx 0;

		.group-info {
			background-color: #fff;
			border-radius: 40rpx 40rpx 0 0;
			min-height: 600rpx;

			.info {
				padding: 40rpx 32rpx;
				display: grid;
				align-items: center;
				grid-template-columns: 1fr 172rpx;

				.name {
					font-size: 48rpx;
					color: #272832;
					font-weight: 600;
					line-height: 66rpx;
					margin-bottom: 20rpx;
				}

				.time {
					font-size: 28rpx;
					color: rgba(39, 40, 50, 0.50);
				}

				.notice {
					grid-column-end: span 2;
					font-size: 28rpx;
					color: #272832;
					line-height: 50rpx;
					display: -webkit-box;
					/* 将对象作为弹性伸缩盒子模型显示 */
					-webkit-box-orient: vertical;
					/* 设置或检索伸缩盒对象的子元素的排列方式 */
					-webkit-line-clamp: 2;
					/* 指定几行 */
					overflow: hidden;
				}

				box-shadow: 0px 1px 0px 0px rgba(0, 0, 0, 0.1);
			}

			.member {
				padding: 22rpx 0;

				.title {
					padding: 0 32rpx;
					display: flex;
					justify-content: space-between;

					.title-left {
						width: 106rpx;
						height: 50rpx;
						font-size: 36rpx;
						color: #272832;
						font-weight: 600;
					}

					.title-right {
						font-size: 32rpx;
						color: rgba(39, 40, 50, 0.60);

						i {
							margin-left: 20rpx;
							font-size: 32rpx;
						}
					}
				}

				.member-list {
					display: flex;
					flex-wrap: wrap;
					padding: 0 15rpx;

					.member-item {
						padding: 20rpx 20rpx 16rpx;

						.avatar {
							position: relative;

							image:nth-child(1) {
								width: 104rpx;
								height: 104rpx;
								border-radius: 20rpx;
							}

							image:nth-child(2) {
								position: absolute;
								top: -16rpx;
								right: -16rpx;
								padding: 10rpx;
								background-color: #FF5D5B;
								width: 20rpx;
								height: 20rpx;
								border-radius: 20rpx;
							}
						}

						.add {
							border-radius: 20rpx;
							background-color: #FFE431;
							padding: 32rpx;
							text-align: center;

							image {
								width: 40rpx;
								height: 40rpx;
							}
						}

						.name {
							text-align: center;
							height: 40rpx;
							font-size: 28rpx;
							color: #272832;
						}
					}
				}
			}

			.group-list {
				.row {
					display: flex;
					padding: 0 26rpx 0 32rpx;
					height: 112rpx;
					font-size: 32rpx;

					.shuxing {
						text-align: center;
						line-height: 124rpx;
						width: 62rpx+32rpx;
						color: #272832;
						font-weight: 400;
						font-size: 32rpx;

						// margin-right: 32rpx;
					}

					.con {
						color: rgba(39, 40, 50, 0.60);
						flex: 1;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
						line-height: 124rpx;
						height: 124rpx;
						padding: 0rpx 32rpx;

						.img {
							width: 108rpx;
							height: 108rpx;
							border-radius: 20rpx;
						}

						Tinput {
							height: 124rpx;
							width: 100%;
						}
					}

					.more {
						text-align: center;
						line-height: 124rpx;

						image {
							vertical-align: center;
							width: 28rpx;
							height: 28rpx;
						}
					}
				}

				.xiaoxi {
					justify-content: space-between;
					align-items: center;
				}
			}
		}
	}

	.midify {
		position: absolute;
		width: 100vw;
		top: 100%;
		left: 0;
		z-index: 999;
		background-color: #fff;
		height: 100%;
		.group-list {
			padding: 20rpx 32rpx;
			.member {
				padding: 20rpx 0;
				height: 80rpx;
				display: flex;
				align-items: center;
		
				.choose {
					text-align: center;
					line-height: 48rpx;
					width: 48rpx;
					height: 48rpx;
					border-radius: 24rpx;
		
		
					.dui {
						font-weight: 600;
					}
				}
		
				.avatar {
					padding: 32rpx;
		
					image {
						width: 80rpx;
						height: 80rpx;
						border-radius: 20rpx;
					}
		
				}
			}
		
		}
		.midify-header {
			display: flex;
			padding: var(--status-bar-height) 32rpx 0;
			width: 750rpx-32rpx-32rpx;
			height: 88rpx;
			background: #FFFFFF;
			box-shadow: 0px 1px 0px 0px rgba(0, 0, 0, 0.1);
			justify-content: space-between;
			align-items: center;
			font-size: 32rpx;
			font-weight: 400;

			.title {
				font-size: 40rpx
			}

			.define {
				color: #4AAAFF;
			}

		}

		.midify-main {
			.midify-content {
				margin: 34rpx auto;
				padding: 22rpx 16rpx;
				box-sizing: border-box;
				width: 686rpx;
				// height: 100rpx;
				height: 386rpx;
				background: #F3F4F6;
				border-radius: 20rpx;
				font-size: 32rpx;
				line-height: 56rpx;
				color: #272832;
				font-weight: 400;
			}
		}
	}

	.group-bottom {
		padding: 12rpx 0;
		position: fixed;
		bottom: 0;
		background-color: #FFF;
		width: 100%;

		.group-btn {
			// background: #FFE431;
			width: 686rpx;
			height: 80rpx;
			margin: auto;
			border-radius: 10rpx;
			text-align: center;
			line-height: 80rpx;
			font-size: 32rpx;
			color: #FF5D5B;
		}
	}
</style>
